<template>
    <div class="config-dialog">
	<el-dialog title="参数设置" v-model="visible" :close-on-click-modal="false" :width="800" custom-class="notpt" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="150px" label-position="right" style="margin-top: 15px;min-height: 300px;">
			<el-row>
                <el-col :span="24">
					<el-form-item label="课程延迟结束分钟" prop="delay_end_time">
                        <el-input v-model="form.delay_end_time" placeholder="请输入课程延迟结束分钟数" clearable>
                            <template #append>分钟</template>
                        </el-input>
                        <div class="tips" style="line-height: 12px;margin-top: 2px;">上课时间到期后，系统自动结束延迟分钟数</div>
                    </el-form-item>
                    <el-form-item label="开课提醒" prop="course_remind_hour">
                        <el-input v-model="form.course_remind_hour" placeholder="请输入开课提醒小时" clearable>
                            <template #append>小时</template>
                        </el-input>
                        <div class="tips" style="line-height: 12px;margin-top: 2px;">开课前几小时提醒教练、学员</div>
                    </el-form-item>
					<el-form-item label="最低购买数量" prop="partner_min_number">
                        <el-input v-model="form.partner_min_number" placeholder="请输入数量" clearable>
                            <template #append>个</template>
                        </el-input>
                        <div class="tips" style="line-height: 12px;margin-top: 2px;">机构合伙人最低购买数量</div>
                    </el-form-item>
					<!-- <el-form-item label="小程序分享图" prop="applet_share_image">
                		<sc-upload v-model="form.applet_share_image" title="上传图片" cropper :aspectRatio="1/1"></sc-upload>
            		</el-form-item> -->
					<el-form-item label="小程序二维码" prop="wechat_applet_qrcode">
                		<sc-upload v-model="form.wechat_applet_qrcode" title="上传图片" :aspectRatio="1/1"></sc-upload>
            		</el-form-item>
                </el-col>
		    </el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    </div>
</template>

<script>

import { in_array } from '@/utils/index.js'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				in_array,
				mode: "add",
				visible: false,
				isSaveing: false,
				form: this.getDefaultRuleForm(),
				//验证规则
				rules: {
					course_remind_hour: [
						{required: true, message: '请输入小时数'},
						{pattern: /^[1-9]\d*$/, message: "只能输入大于0的整数", trigger: "blur" }
					],
					delay_end_time: [
						{required: true, message: '请输入分钟数'},
						{pattern: /^[0-9]\d*$/, message: "只能输入大于或等于0的整数", trigger: "blur" }
					],
					recharge_times: [
						{required: true, message: '请输入充值数量'},
					],
					applet_share_image: [
						{required: false, message: '请上传小程序分享图片'},
					],
					wechat_applet_qrcode: [
						{required: true, message: '请上传小程序二维码'},
					],
					partner_min_number: [
						{required: true, message: '请输入最低购买数量'},
						{pattern: /^[1-9]\d*$/, message: "只能输入大于0的整数", trigger: "blur" }
					]
				},
				recharge_times: [
					{number: 1, amount: 0}
				]
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(){
				this.visible = true;
				return this
			},
			addRechargeTimes(){
				this.form.recharge_times.push({number: 0, amount: 0})
			},
			deleteRechargeTimes(index){
				this.form.recharge_times.splice(index, 1)
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid, fields) => {
					if (!valid) {
						for (let key in fields) {
							if (typeof (fields[key]) !== 'object' || !fields[key][0]) {
								continue;
							}
						}
						return false
					}

					let recharge_times = this.form.recharge_times
					for(let key in recharge_times) {

						let regNeg = /^[1-9]\d*$/
						if (!regNeg.test(recharge_times[key].number)) {
							this.$notify.error('充值数量必须是大于0的整数')
							return false
						}

						regNeg = /^0\.([0-9]|\d[0-9])$|^[0-9]\d{0,8}\.\d{0,2}$|^[0-9]\d{0,8}$/
						if (!regNeg.test(recharge_times[key].amount)) {
							this.$notify.error('充值金额大于等于0且最多两位小数')
							return false
						}

					}


					let arr = []
					for (let i in this.form) {
						arr.push({ key: i, value: this.form[i] })
					}

					try {
						this.isSaveing = true;
						await this.$API.system.config.put({ config: arr }).then(res => {
							this.isSaveing = false;
							if(res.error === 0){
								this.visible = false;
								this.$notify.success("保存成功");
							}
						})
					} catch (error) {
						this.isSaveing = false;
					}
				})
			},
			//表单注入数据
			setData(data) {
				for (let key in data) {
					if (typeof (this.form[key]) != 'undefined') {
						this.form[key] = data[key]
					}
				}
			},
			getDefaultRuleForm() {
				return {
					course_remind_hour: "1",
					delay_end_time: "0",
					// applet_share_image: "",
					wechat_applet_qrcode: "",
					partner_min_number: ""
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
.el-form{
	height: 120px;
}
.title {
    padding-bottom: 20px;
    padding-left: 0px;
}

.mb12 {
	margin-bottom: 12px;
}

.mr10 {
	margin-right: 10px;
}

.tab-pane {
    margin-top: 10px;
}

.config-dialog:deep(.el-dialog):not(.is-fullscreen) {
    min-height: 100px;
}
</style>

